package com.roobtyan.todomemback.service.serviceImpl;

import com.roobtyan.todomemback.entity.MemCard;
import com.roobtyan.todomemback.entity.MemCardExample;
import com.roobtyan.todomemback.entity.ViewCard;
import com.roobtyan.todomemback.entity.ViewCardExample;
import com.roobtyan.todomemback.mapper.MemCardMapper;
import com.roobtyan.todomemback.mapper.ViewCardMapper;
import com.roobtyan.todomemback.service.CardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    @Resource
    private MemCardMapper memCardMapper;
    @Resource
    private ViewCardMapper viewCardMapper;

    @Override
    public List<MemCard> getCardListByDir1Id(int dir1Id) {
        MemCardExample memCardExample = new MemCardExample();
        MemCardExample.Criteria criteria = memCardExample.createCriteria();
        criteria.andDir1IdEqualTo(dir1Id);
        return memCardMapper.selectByExample(memCardExample);
    }

    @Override
    public List<MemCard> selectCardListBySelective(MemCard memCard) {
        MemCardExample memCardExample = new MemCardExample();
        MemCardExample.Criteria criteria = memCardExample.createCriteria();
        if (memCard.getCreateTime() != null) {
            criteria.andCreateTimeEqualTo(memCard.getCreateTime());
        } else if (memCard.getDir1Id() != null) {
            criteria.andDir1IdEqualTo(memCard.getDir1Id());
        } else if (memCard.getDir2Id() != null) {
            criteria.andDir2IdEqualTo(memCard.getDir2Id());
        } else if (memCard.getNextReviewTime() != null) {
            criteria.andNextReviewTimeEqualTo(memCard.getNextReviewTime());
        } else if (memCard.getReviewTime() != null) {
            criteria.andReviewTimeEqualTo(memCard.getReviewTime());
        } else if (memCard.getStatus() != null) {
            criteria.andStatusEqualTo(memCard.getStatus());
        } else if (memCard.getTimeInterval() != null) {
            criteria.andTimeIntervalEqualTo(memCard.getTimeInterval());
        }

        return memCardMapper.selectByExample(memCardExample);
    }

    @Override
    public List<ViewCard> getTodayCard() {
        ViewCardExample viewCardExample = new ViewCardExample();
        ViewCardExample.Criteria criteria = viewCardExample.createCriteria();
        criteria.andNextReviewTimeLessThanOrEqualTo(new Date());
        criteria.andStatusNameEqualTo("正在学习");
        return viewCardMapper.selectByExample(viewCardExample);
    }

    @Override
    public MemCard getCardById(String cardId) {
        return memCardMapper.selectByPrimaryKey(cardId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertSelectiveCard(MemCard memCard) {
        try {
            memCardMapper.insertSelective(memCard);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSelectiveCard(MemCard memCard) {
        try {
            if (memCard.getId() != null) {
                memCardMapper.updateByPrimaryKeySelective(memCard);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteSelectiveCard(String cardId) {
        try {
            memCardMapper.deleteByPrimaryKey(cardId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Date computeReviewDate(int reviewStep) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DATE, reviewStep);
        return calendar.getTime();
    }

    @Override
    public int computeReviewStep(int lastReviewStep) {
        // 计算出与“今天”相隔的时间
        switch (lastReviewStep) {
            case 0:
                return 1;
            case 1:
                return 2;
            case 2:
                return 3;
            case 3:
                return 8;
            case 8:
                return 15;
            case 15:
                return 60;
            case 60:
                return 90;
            case 90:
                return 0;
        }
        return 0;
    }
}
